/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package model2;

import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.net.URL;

/**
 *
 * @author Dustin
 */
public class WebObject {
    protected byte[] content;
    protected URL url;

    public WebObject(URL anUrl, byte[] someContent) {
        content = someContent;
        url = anUrl;
    }

    public void writeToFile() {
        String filepath = "";
        filepath += url.getHost();
        filepath += url.getPath();
        filepath += url.getPath().matches(".*/$") ? "index.html" : "";

        System.out.println("WOB: Starting to write URL: " + url + " to file: " + filepath);
        File outputFile = new File(filepath);

        outputFile.getParentFile().mkdirs();

        try {
            FileOutputStream foStream = new FileOutputStream(outputFile);
            foStream.write(content);
            foStream.close();
        } catch (FileNotFoundException ex) {
            ex.printStackTrace();
        } catch (IOException ex) {
            ex.printStackTrace();
        }
        System.out.println("WOB: Writing file complete.");
    }
}

